package com.sikao.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.AuctionBean;
import com.sikao.app.utils.TimeUtil;

import java.util.List;

/**
 * 拍卖
 */
public class AuctionAdapter extends BaseAdapter {

	private Context context;
	private List<AuctionBean.ResBean.ListBean> beans;

	public AuctionAdapter(Context context, List<AuctionBean.ResBean.ListBean> beans) {
		this.context = context;
		this.beans = beans;
	}

	@Override
	public int getCount() {
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		return beans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.auction_list_item, null);
			convertView.setTag(new ViewHolder(convertView));
		}
		initializeViews((AuctionBean.ResBean.ListBean) getItem(position), (ViewHolder) convertView.getTag());//beans.get(position)
		return convertView;
	}

	private void initializeViews(AuctionBean.ResBean.ListBean listBean, ViewHolder holder) {
		//TODO implement

		//holder.typeTv.setText(listBean.getCat_name());
		holder.typeTv.setText(listBean.getTitle());

		if ("1".equals(listBean.getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_orange);
			holder.releaseTypeTv.setText("仓单挂单");
		}
		if ("2".equals(listBean.getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_blue_light);
			holder.releaseTypeTv.setText("保证金挂单");
		}
		if ("3".equals(listBean.getPublish_type())) {
			holder.releaseTypeTv.setBackgroundResource(R.drawable.shap_yellow);
			holder.releaseTypeTv.setText("信誉挂单");
		}
		holder.timeTv.setText(listBean.getAdd_time_format());
		holder.priceTv.setText(listBean.getPrice());
		holder.priceUnitTv.setText("元/" + listBean.getUnit());
		holder.quantityTv.setText("数量" + listBean.getNumber() + listBean.getUnit());
		if (listBean.getIs_ported().equals("1")) {
			holder.canSplitTv.setVisibility(View.VISIBLE);
		} else {
			holder.canSplitTv.setVisibility(View.GONE);
		}
		holder.deliveryPlaceTv.setText(listBean.getDelivery_area());
		if (listBean.getDelivery_type().equals("1")) {
			holder.endDateTv.setText("买家自提");
		} else {
			holder.endDateTv.setText("送货上门");
		}
	}

	protected class ViewHolder {
		private TextView typeTv;//所属分类
		private TextView nameTv;//名称
		private TextView releaseTypeTv;//挂单方式
		private TextView timeTv;//时间
		private TextView canSplitTv;//是否可拆分
		private TextView priceTv;//价格
		private TextView priceUnitTv;//价格单位（吨或台）不用加元，布局中已加
		private TextView quantityTv;//数量
		private TextView deliveryPlaceTv;//交割地
		private TextView endDateTv;//截止时间

		public ViewHolder(View view) {
			typeTv = (TextView) view.findViewById(R.id.type_tv);
			nameTv = (TextView) view.findViewById(R.id.name_tv);
			releaseTypeTv = (TextView) view.findViewById(R.id.release_type_tv);
			timeTv = (TextView) view.findViewById(R.id.time_tv);
			canSplitTv = (TextView) view.findViewById(R.id.can_split_tv);
			priceTv = (TextView) view.findViewById(R.id.price_tv);
			priceUnitTv = (TextView) view.findViewById(R.id.price_unit_tv);
			quantityTv = (TextView) view.findViewById(R.id.quantity_tv);
			deliveryPlaceTv = (TextView) view.findViewById(R.id.delivery_place_tv);
			endDateTv = (TextView) view.findViewById(R.id.end_date_tv);
		}
	}
}
